<template>
  <div style="text-align: center;">
    <el-form label-position="left" class="userInfo-container" label-width="100px" :model="user">
      <el-form-item label="用户名：" style="height: 70px; margin-bottom: 0px; text-align: left">
        <div style="display: flex">
          <label style="color: gray;text-align: left;width: 70%">{{user.username}}</label>
          <el-upload
            class="avatar-uploader"
            action="https://jsonplaceholder.typicode.com/posts/"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
            <img v-if="user.iconUrl" :src="user.iconUrl" class="avatar" style="border-radius: 30px"
            >
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </div>
      </el-form-item>
      <el-form-item label="昵称：" style="text-align: left">
        <el-input v-model="user.name" suffix-icon="el-icon-edit"></el-input>
      </el-form-item>
      <el-form-item label="性别：" style="text-align: left">
        <div style="width: 300px">
          <el-radio v-model=radio label='0' style="width: 30%">男</el-radio>
          <el-radio v-model=radio label='1'>女</el-radio>
        </div>
      </el-form-item>
      <el-form-item label="绑定邮箱：" style="text-align: left">
        <el-input v-model="user.email" suffix-icon="el-icon-edit"></el-input>
      </el-form-item>
      <el-form-item label="个人介绍：" style="text-align: left">
        <el-input type="textarea" v-model="user.introduction" :autosize="{ minRows: 4, maxRows: 5}"
                  suffix-icon="el-icon-edit"></el-input>
      </el-form-item>
      <el-form-item label="创建时间：" style="text-align: left">
        <label style="color: gray;text-align: left;width: 70%">{{createTime}}</label>
      </el-form-item>
      <el-form-item label="身份：" style="text-align: left">
        <label style="color: gray; width: 70%">{{status}}</label>
      </el-form-item>
      <el-button type="primary" @click="submitClick">保存</el-button>
      <!-- <el-button type="primary" @click="submitClick" style="margin-left: 165px;">保存</el-button> -->
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'UserInfo',
  data () {
    return {
      user: {},
      filedata: {},
      radio: '0'
    }
  },
  methods: {
    handleAvatarSuccess (res, file) {
      this.user.iconUrl = URL.createObjectURL(file.raw)
      // console.log(file)
    },
    beforeAvatarUpload (file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      // 校验成功上传文件
      if (isJPG && isLt2M == true) {
        // console.log(file)

        // 将文件转化为formdata数据上传
        this.filedata = file
        // console.log(this.filedata)
      }
      return isJPG && isLt2M
    },
    submitClick () {
      let that = this
      // console.log(this.user)
      var file = new FormData()
      this.user.gender = this.radio
      file.append('pic', this.filedata)
      file.append('id', this.user.id)
      file.append('name', this.user.name)
      file.append('email', this.user.email)
      file.append('gender', this.user.gender)
      file.append('introduction', this.user.introduction)

      that.uploadFileRequest('/api/user/update', file)
        .then(resp => {
          if (resp && resp.status == 200) {
            that.$message.success('保存成功！')
            that.$store.commit('login', resp.data.data)
            that.user = JSON.parse(window.localStorage.getItem('user' || '[]'))
          } else {
            that.$message.error('信息保存失败，请重新保存！')
          }
        })
    }
  },
  created () {
    this.user = JSON.parse(window.localStorage.getItem('user' || '[]'))
    this.radio = this.user.gender.toString()
    // console.log(this.user)
  },
  computed: {
    status () {
      if (this.user.status == 0) {
        return '学生'
      } else if (this.user.status == 1) {
        return '教师'
      } else if (this.user.status == 2) {
        return '管理员'
      } else {
        return ''
      }
    },
    createTime () {
      return new Date(this.user.createTime).toLocaleDateString()
    }
  }
}
</script>

<style scoped>

  .userInfo-container {
    border-radius: 15px;
    background-clip: padding-box;
    margin: auto;
    margin-top: 20px;
    width: 400px;
    padding: 35px 35px 15px 35px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
  }

  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 50px;
    height: 50px;
    line-height: 50px;
    text-align: center;
  }

  .avatar {
    width: 50px;
    height: 50px;
    display: block;
  }
</style>
